﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BiliBiliCSharpStudy.old {
    /**
     * unary operator
     * 一元运算符
     * 单目运算符
     * 
     */
    class UnaryOperatorDemo {
        public static void demo() {
            //test1();
            //test2();
            //test3();
            test4();
        }

        public static void test4() {
            Student5 stu = new Student5(null);
            Console.WriteLine(stu.Name);
        }

        public static void test3() {
            int x = 12345678;
            int y = ~x; // 取反
            Console.WriteLine(x);
            Console.WriteLine(y);
            string xStr = Convert.ToString(x, 2).PadLeft(32, '0');
            string yStr = Convert.ToString(y, 2).PadLeft(32, '0');
            Console.WriteLine(xStr);
            Console.WriteLine(yStr);
        }

        public static void test2() {
            int x = int.MaxValue;
            int y = int.MinValue;
            Console.WriteLine(x);
            Console.WriteLine(y);
            try {
                int z = checked(-y);
                Console.WriteLine(z);
            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }

        }

        public static void test1() {
            int x = 100;
            Console.WriteLine("x: " + x);
            int y = -x;
            Console.WriteLine("y: " + y);
            y = -(-x);
            Console.WriteLine("y: " + y);
        }
    }

    class Student5 {
        public Student5(string initName) {
            if (!string.IsNullOrEmpty(initName)) {
                this.Name = initName;
            } else {
                throw new ArgumentException("initName can't be empty or null");
            }
        }
        public string Name;
    }
}
